package com.mc.servlet;

import com.mc.bean.ST;
import com.mc.bean.Student;
import com.mc.bean.TT;
import com.mc.bean.Teacher;
import com.mc.service.MatchVOService;
import com.mc.service.STService;
import com.mc.service.TTService;
import com.mc.service.impl.MatchVOServiceImpl;
import com.mc.service.impl.STServiceImpl;
import com.mc.service.impl.TTServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author wangcf
 * @version 1.0
 * @Description 团队添加人员控制器
 * @date 2023/2/16 11:52
 */
@WebServlet("/teamaddpeople.action")
public class TeamAddPeopleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TTService ttService = new TTServiceImpl();
        STService stService = new STServiceImpl();
        MatchVOService matchVOService=new MatchVOServiceImpl();
        //获取要加入的队伍id
        int teamid = Integer.parseInt(req.getParameter("teamid"));
        HttpSession session = req.getSession();
        //获取要加入这个队伍的用户
        Object obj = session.getAttribute("user");
        String midStr=req.getParameter("mid");
        int mid=Integer.parseInt(midStr);
        //指示灯,表示不存在
        boolean flag = false;
        if (obj instanceof Teacher) {
            Teacher teacher = (Teacher) obj;
            //获取这个团队与用户关系对象集合，判断这个用户是否已经加入了这个队伍
            List<TT> ttList = ttService.findByTeamId(teamid);
            for (TT tt : ttList) {
                if (teacher.getTid() == tt.getTid()) {
                    //已经加入过了给出提示
                    flag = true;
                }
            }
            //没加入过，进行添加
            if (flag) {
                req.setAttribute("mess", "已经存在了");
                req.setAttribute("mid",mid);
                req.getRequestDispatcher("teampeople.action?teamid=" + teamid).forward(req, resp);
            } else {
                //如果用户在这个队伍不存在，还要查用户的其他队伍有没有加入这个赛事
                //写过，去借
                boolean i = ttService.add(new TT(teamid, teacher.getTid()));
                System.out.println(i);
                resp.sendRedirect("teampeople.action?teamid="+teamid+"&mid="+mid);
            }
        } else if (obj instanceof Student) {
            Student student = (Student) obj;
            //学生赛事只能参加一次，判断他是否参与过

            System.out.println("aa"+mid);
            List<Student> studentList=matchVOService.findByMidInfoStu(mid);
            for (Student student1 : studentList) {
                //判断用户是否报名过了
                if (student.getSid() == student1.getSid()) {
                    flag = true;
                }
            }
            //没加入过，进行添加
            if (flag) {
                req.setAttribute("mess", "已经存在了");
                req.setAttribute("mid",mid);
                req.getRequestDispatcher("teampeople.action?mess="+"已经存在了"+"&teamid=" + teamid).forward(req, resp);
            } else {
                boolean i = stService.add(new ST(teamid, student.getSid()));
                System.out.println(i);
                resp.sendRedirect("teampeople.action?teamid=" + teamid+"&mid="+mid);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
